﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SetSailDal
{
    /// <summary>
    /// 数据库访问接口
    /// </summary>
    public class DBHelper : IDisposable
    {
        private static string QHSqlServer;

        public DBHelper()
        {
            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["QHSqlServer"];
            if (settings != null)
                QHSqlServer = settings.ConnectionString;
            if (string.IsNullOrWhiteSpace(QHSqlServer))
                throw new Exception("SqlServer数据库连接字符串异常");
        }
        public DBHelper(string connectString)
        {

            QHSqlServer = connectString;
        }
        // 绣花数据库连接字符串
        public static string XHDBConnectionString
        {
            get
            {
                if (string.IsNullOrWhiteSpace(QHSqlServer))
                    throw new Exception("SqlServer数据库连接字符串异常");
                return QHSqlServer;
            }
        }

        private IDbConnection _DbContext;
        public IDbConnection DbContext
        {
            get
            {
                if (_DbContext == null)
                {
                    _DbContext = new SqlConnection(QHSqlServer);
                    _DbContext.Open();
                }
                return _DbContext;
            }
        }

        //释放资源
        public void Dispose()
        {
            if (DbContext != null)
            {
                DbContext.Close();
                DbContext.Dispose();
            }
        }
    }
}
